package graphs;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

/**
 * generates a bar graph for the given data
 * 
 * @author lindsaykubasik
 * 
 */
@SuppressWarnings("serial")
public class BarGraph extends AbstractGraph<String, Double>
{
    /**
     * Constructor for bar graph
     */
    public BarGraph()
    {
        myData = null;
        myLabels = null;
        myXvals = null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    public void paintComponent(Graphics pen)
    {
        Dimension canvas = getSize();
        pen.drawString(myLabels.get(0), (canvas.width / 2), 20);
        int k = 0;
        Double yVal = 0.0;
        List<Color> colors = new ArrayList<Color>();
        double[] yMinMax = findYMinMax(myData, myXvals, colors);
        double yMax = yMinMax[0];

        Dimension bounds = getSize();
        for (Object x : myXvals)
        {
            yVal = (Double) myData.get(x).get(0);
            double percent = yVal / yMax;
            Dimension size = new Dimension(bounds.width / myXvals.size(),
                    (int) ((bounds.height - 100) * percent));
            Point topLeft = new Point(k * size.width + 5, bounds.height
                    - size.height - 25);
            if (yVal > 0)
            {
                pen.setColor(colors.get(0));
                pen.fillRect(topLeft.x, topLeft.y, size.width, size.height);
                pen.setColor(Color.black);
                pen.drawRect(topLeft.x, topLeft.y, size.width, size.height);
                pen.setColor(Color.white);
                pen.drawString("" + yVal, topLeft.x + 20, topLeft.y
                        + (size.height / 2) - 10);
                pen.setColor(Color.BLACK);
                pen.drawString((String) x, topLeft.x, bounds.height - 10);
            }
            k++;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see graphs.AbstractGraph#toString()
     */
    public String toString()
    {
        return "BarGraph";
    }
}